﻿#include "FileMesh.h"
#include "DataObject.h"
#include "Logging.h"
#include "MeshTool.h"

#include <libmesh/communicator.h>
#include <libmesh/mesh.h>
#include <libmesh/namebased_io.h>
#include <libmesh/mesh_refinement.h>
#include <libmesh/boundary_info.h>

#include <vtkm/cont/Timer.h>

FileMesh::FileMesh(const Configuration& cfg)
  : Mesh(cfg)
  , _mesh_file(Get<std::string>("file"))
{
  //_boundary_ids = GetVectorValue<int>("boundary_id");
  //_boundary_names = GetVectorValue<std::string>("boundary_name");
}


void FileMesh::Build()
{
  libMesh::Parallel::Communicator comm;
  libMesh::Mesh mesh(comm);

  vtkm::cont::Timer timer;
  timer.Start();
  try
  {
    libMesh::NameBasedIO(mesh).read(_mesh_file);
    //mesh.prepare_for_use();
    auto& boundary_info = mesh.get_boundary_info();
     
    //if (_boundary_ids.size() != _boundary_names.size())
    //{
    //  console::Error("boundary_id，boundary_name的个数不同");
    //}
    //for (size_t i = 0; i < _boundary_ids.size(); i++)
    //{
    //  boundary_info.sideset_name(_boundary_ids[i]) = _boundary_names[i];
    //}

    //libMesh::MeshRefinement mesh_refinement(mesh);
    //mesh_refinement.uniformly_refine(2);

    boundary_info.print_summary();

  }
  catch (const std::exception& e)
  {
    console::Error(e.what());
  }
  console::Info("  1-1.  读取网格: ", timer.GetElapsedTime());
  
  // prepare_for_use的作用有：分区，单元连接、拓扑重排
  //mesh.prepare_for_use();

  timer.Start();
  MeshTool::Build(*this, mesh);
  console::Info("  1-2.  复制拓扑: ", timer.GetElapsedTime());

  Prepared();
}

